package com.programmingeyeclient.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import com.programmingeyeclient.user.common.Result;
import com.programmingeyeclient.user.constants.Constants;
import com.programmingeyeclient.user.constants.ResultEnum;
import com.programmingeyeclient.user.entity.EduStudent;
import com.programmingeyeclient.user.mapper.EduStudentMapper;
import com.programmingeyeclient.user.service.EduStudentService;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;

@Service
public class EduStudentServiceImpl implements EduStudentService {

    @Resource
    private EduStudentMapper eduStudentMapper;


    @Override
    public Result findByUserName(EduStudent eduStudent) {
        QueryWrapper<EduStudent> wrapper = new QueryWrapper<>();
        wrapper.eq("mobile", eduStudent.getMobile());
        EduStudent eduStudentLogin = eduStudentMapper.selectOne(wrapper);
        //判断账号是否存在
        if (eduStudentLogin != null && eduStudentLogin.getMobile().equals(eduStudent.getMobile())){
            //判断用户是否被冻结--1是已冻结，2是正常的用户
            if (eduStudentLogin.getIsAvalible() == 1) {
                return new Result(Constants.EST_NOT, ResultEnum.FAIL_ACCOUNT_LOCK.getMsg(), null);
            } else {
                //判断密码是否正确
                if (eduStudentLogin != null && eduStudentLogin.getPassword().equals(eduStudent.getPassword())) {
                    return new Result(Constants.EST_YES, ResultEnum.SUCCESS.getMsg(), eduStudentLogin);
                } else {
                    return new Result(Constants.est_SEX_WOMEN, ResultEnum.FAIL_HAVE_NOT_EXIST.getMsg(), null);
                }
            }
        }else {
            return new Result(Constants.est_SEX_MAN, ResultEnum.FAIL_ACCOUNT_NOT_EXIST.getMsg(), null);
        }

    }

    @Override
    public Result enroll(EduStudent eduStudent) {
        QueryWrapper<EduStudent> wrapper = new QueryWrapper<>();
        wrapper.eq("mobile", eduStudent.getMobile());
        EduStudent eduStudentLogin = eduStudentMapper.selectOne(wrapper);
        //判断手机号是否被注册了
        if (!ObjectUtils.isEmpty(eduStudentLogin)) {
            //判断大于0就注册成功
            if (eduStudentMapper.enroll(eduStudent) > 0) {
                return new Result(Constants.EST_YES, ResultEnum.SUCCESS.getMsg(), eduStudentLogin);
            } else {
                return new Result(Constants.EST_YESS, ResultEnum.FAIL.getMsg(), eduStudentLogin);
            }
        } else {
            return new Result(Constants.SOURCE_TYPE_APP, ResultEnum.FAIL_NOT_FIND_PAGE.getMsg(), null);
        }
    }

    @Override
    public Boolean getUpdate(EduStudent eduStudent) {
        return eduStudentMapper.getUpdate(eduStudent) > 0;
    }

    @Override
    public EduStudent getStudentid(Integer id) {
        return eduStudentMapper.getStudentid(id);
    }
}
